<template>
	<view>
		<view>
			<view class="order-price">￥<text>{{Number(info.money).toFixed(2)}}</text></view>
			<view class="order-no">{{info.guiname}}</view>
		</view>
		<view class="daboxs">
			<uni-section title="充值手机号" type="line" titleColor="#686868" iconColor="#686868"></uni-section>
			<view class="tit"> </view>
			<uni-easyinput v-model="tel" placeholder="请输入充值手机号" type="number" maxlength="11"></uni-easyinput>
		</view>
		<view class="tvbox">
			<view class="buycss" @click="orderBuy"> 下单购买 </view>
		</view>
	</view>
</template>

<script setup>
	import { ref , onMounted } from 'vue';
	import { onShow , onLoad } from '@dcloudio/uni-app';
	import { apiXiadan , apiBuy } from '@/api/upapi.js';
	
	const info = ref({});
	const payinfo = ref({});
	const xwmemberInfo = ref({});
	const tel = ref('');
	const imgUrls = ref('https://m.aqbwg.cn/sourse/xiaowan/product/');
	const mervalue = uni.getStorageSync('xwmemberId');
	
	const orderBuy = async() => {
		if( !tel.value ){ uni.showToast({ title: '请输入充值手机号',icon:'none' }); return; };
		try {
			let res = await apiXiadan({ 
				proid: info.value.pid,
				memberid:mervalue,
				num:1,
				tel:tel.value,
				skuId:info.value.skuId
			});
			if(res.data.result==0){
				payinfo.value=res.data.object;
				buy();
			}else{
				uni.showToast({ title: res.data.msg,icon:'none' });
			}
		} catch (error) {
			console.error('获取数据失败', error);
		}
	};
	const buy = async() => {
		try {
			let res = await apiBuy({ 
				memberId: mervalue,
				merchantId:133,
				type:2,
				bid:payinfo.value.bid,
				openid:xwmemberInfo.value.openid
			});
			if (res.data.result == 0) {
				var data = JSON.parse(res.data.object);
				uni.requestPayment({
					appId: data.appId,
					provider: 'wxpay',
					timeStamp: data.timeStamp,
					nonceStr: data.nonceStr,
					package: data.package,
					signType: data.signType,
					paySign: data.paySign,
					success: function(res) {
						uni.showToast({ title: '支付成功', icon: 'none' })
						setTimeout(function() {
							uni.navigateBack(1)
						}, 1500);
					},
					fail: function(err) {}
				});
			} else {
				uni.showToast({ title: res.data.msg,icon:'none'  });
			}
		} catch (error) {
			console.error('获取数据失败', error);
		}
	};
	
	onLoad((option) => {
		Object.assign(info.value, option);
		console.log('op===',option);
		xwmemberInfo.value = uni.getStorageSync('xwmemberInfo');
	});
</script>

<style>
	page{ background-color: #fff; }
</style>
<style lang="scss" scoped>
	.daboxs{
		padding: 30rpx;
		.tit{
			height: 14rpx;
			width: 100%;
		}
	}
	.order-price {
		text-align: center;
		color: #ff0000;
		font-size: 40rpx;
		padding-top: 40rpx;
	
		text {
			font-size: 80rpx;
			font-weight: bold;
		}
	}
	.order-no {
		text-align: center;
		color: #959CA6;
		font-size: 24rpx;
		margin-top: 12rpx;
		padding-bottom: 20rpx;
	}
	.tvbox{
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 30rpx 0;
		
		.buycss{
			background-color: #ff4a82;
			padding:24rpx;
			border-radius: 50rpx;
			font-size: 28rpx;
			margin: 0 30rpx;
			text-align: center;
			font-weight: bolder;
			color: #fff;
		}
	}
</style>
